package com.example.service.impl;

import com.example.dao.UserTypeDao;
import com.example.entity.result.Result;
import com.example.entity.vo.UserType;
import com.example.service.UserTypeService;
import com.example.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserTypeServiceImpl implements UserTypeService {
    @Autowired
    private UserTypeDao userTypeDao;

    //查询
    @Override
    public Result query(String key, String value) {
        DataUtils.paramCheckEmpty(key, value);
        switch (key) {
            case "all":
                return DataUtils.getResult(Result.SUCCESS, "userTypeALl",
                        userTypeDao.selectAll());
            case "byId":
                return DataUtils.getResult(Result.SUCCESS, "userType",
                        userTypeDao.selectById(DataUtils.paramToInteger(value)));
            case "byName":
                return DataUtils.getResult(Result.SUCCESS, "userType",
                        userTypeDao.selectByName(value));
            case "byLike":
                return DataUtils.getResult(Result.SUCCESS, "userType",
                        userTypeDao.selectByLike(value));
            default:
                return DataUtils.getResult(Result.PARAMETER_INVALID);
        }
    }

    //添加
    @Override
    public Result insert(UserType userType) {
        if (StringUtils.isEmpty(userType.getAName())) {
            return DataUtils.getResult(Result.PARAMETER_EMPTY);
        }
        if (userTypeDao.selectByName(userType.getAName()) != null) {
            return DataUtils.getResult(Result.ERROR, "该类型已存在");
        }
        int add = userTypeDao.add(userType);
        return (add > 0) ? DataUtils.getResult(Result.SUCCESS) : DataUtils.getResult(Result.ERROR);
    }

    //更新
    @Override
    public Result update(UserType userType) {
        if (StringUtils.isEmpty(userType.getAName())) {
            return DataUtils.getResult(Result.PARAMETER_EMPTY);
        }
        if (userTypeDao.selectByName(userType.getAName()) != null) {
            return DataUtils.getResult(Result.ERROR, "该类型已存在");
        }
        if (DataUtils.dataCompare(userType, userTypeDao.selectById(userType.getAid()), UserType.class) == 1) {
            return DataUtils.getResult(Result.SUCCESS, "无更新");
        }
        int update = userTypeDao.update(userType);
        return (update > 0) ? DataUtils.getResult(Result.SUCCESS) : DataUtils.getResult(Result.ERROR);
    }

    //删除
    @Override
    public Result delete(int aid) {
        if (userTypeDao.selectById(aid) == null) {
            return DataUtils.getResult(Result.ERROR, "找不到该用户类型");
        }
        int delete = userTypeDao.delete(aid);
        return (delete > 0) ? DataUtils.getResult(Result.SUCCESS) : DataUtils.getResult(Result.ERROR);
    }

}
